package com.outfood.utils;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfoodxia-master
 * @Package com.outfood.utils
 * @Date 2024-05-17 10:44
 * @Version 1.0
 */
public class JavaScriptUtil {
    // 创建一个ScriptEngineManager实例
    public static ScriptEngineManager manager = new ScriptEngineManager();
    // 获取JavaScript引擎
    public static ScriptEngine engine = manager.getEngineByName("nashorn");


    public static String createRequestId() {

        try {
            // 执行JavaScript代码
            Object result = engine.eval("\t\tfunction v() {\n" +
                    "\t\t    return \"xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx\".replace(/[xy]/g, (function(e) {\n" +
                    "\t\t        var t = 16 * Math.random() % 16 | 0;\n" +
                    "\t\t        return (\"x\" === e ? t : 3 & t | 8).toString(16)\n" +
                    "\t\t    }\n" +
                    "\t\t    )).toUpperCase() + \"|\" + Date.now()\n" +
                    "\t\t} v()");
            // 输出JavaScript返回值
            return result.toString();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//        String s = textDecode("JTdCJTIybWVzc2FnZSUyMjolMjJbJTdCJTVDJTIyY29udGVudCU1QyUyMjolN0IlNUMlMjJkYXlTbiU1QyUyMjoxNCwlNUMlMjJvcmRlcklkcyU1QyUyMjpbJTVDJTIyODA1ODQzMDE1MDMxMjcxMjU3NiU1QyUyMl0sJTVDJTIyc2VuZFRpbWUlNUMlMjI6MTcxNzA4MTAwNDAwMCwlNUMlMjJzaG9wSWQlNUMlMjI6NTE4MjE1MjQ4JTdELCU1QyUyMm1zZ0lkJTVDJTIyOiU1QyUyMjYxMTAwMDAwNjk0Mzc1MTU4NzMlNUMlMjIsJTVDJTIyb2xkVG9waWMlNUMlMjI6JTVDJTIyb3JkZXIub3JkZXIudW5wcm9jZXNzJTVDJTIyLCU1QyUyMnJlcXVpcmVkJTVDJTIyOnRydWUsJTVDJTIydG9waWMlNUMlMjI6JTVDJTIyVFJBREUuRVNDUk9XLk9SREVSLldBSVQuQUNDRVBUJTVDJTIyJTdEXSUyMiU3RA==");
//        System.out.println(s);
//    }


    public static String textDecode(String data) {

        try {
            // 执行JavaScript代码
            Object result = engine.eval("a = function(e) {\n" +
                    "        try {\n" +
                    "            if (\"undefined\" != typeof window && atob)\n" +
                    "                return atob(e)\n" +
                    "        } catch (e) {\n" +
                    "            console.log(e)\n" +
                    "        }\n" +
                    "        var t = String(e).replace(/[=]+$/, \"\");\n" +
                    "        if (t.length % 4 == 1)\n" +
                    "            throw new Error(\"'atob' failed: The string to be decoded is not correctly encoded.\");\n" +
                    "        for (var n = \"\", r = 0, o = void 0, a = void 0, i = 0; a = t.charAt(i++); ~a && (o = r % 4 ? 64 * o + a : a,\n" +
                    "        r++ % 4) ? n += String.fromCharCode(255 & o >> (-2 * r & 6)) : 0)\n" +
                    "            a = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\".indexOf(a);\n" +
                    "        return n\n" +
                    "    },\n" +
                    "\t\n" +
                    "\t   utf8ToUtf16= function(e) {\n" +
                    "\t            if (e) {\n" +
                    "\t                var t, n, r, o = [], a = e.length;\n" +
                    "\t                for (t = 0; t < a; t++)\n" +
                    "\t                    (n = []).push(e.charCodeAt(t)),\n" +
                    "\t                    0 == (n[0] >> 7 & 255) ? o.push(e.charAt(t)) : 6 == (n[0] >> 5 & 255) ? (n.push(e.charCodeAt(++t)),\n" +
                    "\t                    (r = []).push(31 & n[0]),\n" +
                    "\t                    r.push(63 & n[1]),\n" +
                    "\t                    o.push(String.fromCharCode(r[0] << 6 | r[1]))) : 14 == (n[0] >> 4 & 255) && (n.push(e.charCodeAt(++t)),\n" +
                    "\t                    n.push(e.charCodeAt(++t)),\n" +
                    "\t                    (r = []).push(n[0] << 4 | n[1] >> 2 & 15),\n" +
                    "\t                    r.push((3 & n[1]) << 6 | 63 & n[2]),\n" +
                    "\t                    o.push(String.fromCharCode(r[0] << 8 | r[1])));\n" +
                    "\t                return o.join(\"\")\n" +
                    "\t            }\n" +
                    "\t        },\n" +
                    "\n" +
                    "\t\t\tdecodeURI(utf8ToUtf16(a('"+data+"')))");
            // 输出JavaScript返回值
            return result.toString();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }

}
